<template>
	<el-container v-loading="loading">
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="tabChange">
			</sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<fsButton ref="button" plain size="small" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
			</div>
			<div class="right-panel">
				<ex-search ref="search" :searches="searches" @searchOk="searchHandler" :fixedParam="{}"></ex-search>
			</div>
		</el-header>
		<el-main class="nopadding">
			<ex-table id="assetTable" :height="0"  :selection="false" ref="table"
					  :columns="columns" :url="this.$API.asset.assetStatisticsReport.countAssetIncrease.url"
					  :auto-refresh="false" :refs="this.$refs" search="search"
					  :pagination="false"
					  :params="{}" @columnClick="columnClickHandler"></ex-table>
		</el-main>
	</el-container>
	<data-card ref="dataCard"></data-card>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExTreeNavigation from "@/components/exSearchTree/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import scSelectFilter from "@/components/scSelectFilter/index.vue";
import DataCard from "@/views/asset/card/card.vue";
import scEcharts from "@/components/scEcharts/index.vue";
import print from "@/utils/print";
export default {
	name: 'column',
	components: {
		scEcharts,
		DataCard,
		scSelectFilter,
		ExSearchTree,
		ExTreeNavigation,
		ExSearch,
		ExTable,
	},
	data() {
		return {
			dialog: {
				save: false,
				permission: false,
			},
			apiObj: [],
			buttons: [
				{name: "导出", icon: "el-icon-download", click: "export", permission: ""},
				{name: "打印", icon: "el-icon-download", click: "print", permission: ""},
			],
			searches: [
				{type: "tree-select", name: "管理部门", code: "dutyDepartmentIds", placeholder: "请选择管理部门", dictCode:'SysOrganization',checkStrictly:false,multiple:true},
				{type: "tree-select", name: "使用部门", code: "usingDepartmentIds", placeholder: "请选择使用部门", dictCode:'SysOrganization',checkStrictly:false,multiple:true},
				{type: "tree-select", name: "资产类别", code: "assetCategoryIds", placeholder: "请选择资产类别", dictCode:'FamsAssetCategory',checkStrictly:false,multiple:true},
				{type: "daterange", name: "购置日期", code: "boughtDateStart,boughtDateEnd", placeholder: "请选择购置日期", formatter:"YYYY-MM-dd",valueFormat:"YYYYMMDD",range:30},
			],
			columns: [
				{code: "name", name: "部门名称",click:"show1", width:""},
				{code: "assetCount", name: "增量数量",sortable:true, width:"200",summary:true},
				{code: "originalValue", name: "金额",sortable:true, width:"200",summary:true},
			],
			filterData: [
				{
					title: "统计方式",
					key: "assetStatisticsType",
					multiple: false,
					options: [
						{
							label: "按管理部门统计",
							value: "1"
						},
						{
							label: "按资产分类统计",
							value: "2"
						},
					]
				},
			],
			parentLevelCode:"",
			assetIds:[],
			loading: false,
			assetData:[],
			assetStatisticsType: "1",
			readDeptIds: this.$TOOL.current.readDeptIds(),
			manageCategoryIds: this.$TOOL.current.manageAssetCategoryIds(),
			assetCategoryList:[],
			deptList:[],
			loginUsers: this.$TOOL.current.user(),
		}
	},
	created() {
	},
	mounted() {
		this.searchHandler();

	},
	beforeMount() {
	},
	methods: {
		buttonHandler(index, name) {
			this.$options.methods[name](this, index);
		},
		async searchHandler() {
			let queryParams = {}
			this.formateQueryParams(queryParams,this);
			await this.$refs.table.refresh(queryParams);
		},
		columnClickHandler(name,data){
			this.$options.methods[name](this, data);
		},
		//导出
		async export(_this,index) {
			_this.$refs.button.loading(index, true);
			let queryParams = {}
			_this.formateQueryParams(queryParams,_this);
			queryParams.exportType = "increase";
			let res = await _this.$API.asset.assetStatisticsReport.export.post(queryParams);
			if (res.code === 2000) {
				await _this.$alert(_this.$t('common.export_request_success'), "提示", {type: 'success'}).catch(()=>{});
			}
			_this.$refs.button.loading(index, false);
		},
		//标签切换
		async tabChange(name){
			this.assetStatisticsType = name.assetStatisticsType;
			if(this.assetStatisticsType === 1){
				this.columns[0].name = "部门名称";
			}else{
				this.columns[0].name = "资产名称";
			}
			await this.searchHandler();
		},
		print(){
			//直接传入REF或者querySelector
			window.print();
		},
		show1(_this,row){
			let queryParams = {};
			if(!queryParams.boughtDateStart){
				queryParams.boughtDateStart = "20000101";
			}
			_this.formateQueryParams(queryParams,_this);
			if(_this.assetStatisticsType === "1") {
				queryParams.dutyDepartmentId = row.id;
			}else if(_this.assetStatisticsType === "2"){
				queryParams.assetCategoryId = row.id;
			}
			_this.searchAsset(row.name,queryParams);
		},
		searchAsset(item,queryParams) {
			let title = "增长统计"+item+"资产明细";
			//跳转到搜索台账页面
			let fromRouteId = this.$TOOL.crypto.MD5(this.$route.path);//当前页面的路由地址
			let key = this.$TOOL.crypto.MD5(title);//可以做为key的关键字段或内容
			/**
			 *  1 = 根据管理部门，根据登录账户管理范围，资产的管理部门在账户管理范围内的显示<br>
			 *  2 = 根据使用部门，根据登录账户管理范围，资产的使用部门在账户管理范围内的显示<br>
			 *  3 = 根据管理部门或使用部门，根据登录账户管理范围，资产的使用部门或管理部门在账户管理范围内的显示<br>
			 *  其它 = 根据管理部门，按台账，根据使用部门或管理部门或借用部门在管理范围内的
			 */
			//设置搜索参数
			let routeParams = {
				id: key,
				title: title,
				fixedParams:{
				},//固定搜索参数，打开页面后，用户通过搜索界面的清空按钮无法清除
				searchParams: queryParams
			};
			this.$store.dispatch("addRouteParams", routeParams);
			this.$router.push({
				name: "toAssetList",
				params: {fromRouteId: fromRouteId, key: key}
			});
		},
		formateQueryParams(queryParams,_this){
			queryParams.assetStatisticsType = _this.assetStatisticsType;
			Object.assign(queryParams, _this.$refs.search.searchParams());
			if(!queryParams.dutyDepartmentIds||queryParams.dutyDepartmentIds.length===0){
				queryParams.dutyDepartmentIds = [];
				if(!_this.readDeptIds || _this.readDeptIds.length===0){
					queryParams.dutyDepartmentIds.push(_this.loginUsers.orgId);
				}else{
					queryParams.dutyDepartmentIds = _this.readDeptIds;
				}
			}
			if(!queryParams.usingDepartmentIds||queryParams.usingDepartmentIds.length===0){
				queryParams.usingDepartmentIds = [];
				queryParams.usingDepartmentIds = _this.readDeptIds;
			}
			if(!queryParams.assetCategoryIds || queryParams.assetCategoryIds.length===0){
				queryParams.assetCategoryIds = _this.manageCategoryIds;
			}
		}
	}
}

</script>
<style scoped>

</style>
